import "@/utils/regexp";
import "@/utils/alertTips";
import "@/utils/dialog";
import "@/utils/slideUpDialog";
import "@/utils/zrzb";
import "@/utils/loginModal";

//自动适配页面函数
// ~function resizeFontsize(){
// 	let $body = $("html,body");
//     $(window).on("resize",function(){
//         let width = $(window).width();
//         if(width > 475){
//             width = 475
//         }
//         if(width < 320){
//             width = 320
//         }
//         $body.css({
//             "font-size":`${Math.round(width / 375 * 20)}px`
//         })
        
//     })
//     $(window).resize();
//     let timer = setTimeout(() => {
//         clearTimeout(timer);
//         $body.removeClass("opacity");
//     },100)
    
// }();


//调度任务
(function() {
    function TaskManager (callback,time) {
        this.timer = undefined;
        this.callback = callback;
        this.time = time;
    }
    TaskManager.prototype.exec = function () {
        this.callback()
    }

    TaskManager.prototype.run = function (nowrun) {
        if(nowrun){
            this.exec()
        }
        clearTimeout(this.timer);
        this.timer = setInterval(this.callback,this.time * 1000)
    }
    TaskManager.prototype.stop = function (nowrun) {
        if(nowrun){
            this.exec()
        }
        this.timer && clearTimeout(this.timer);
    }



    window.TaskManager = TaskManager;
})()



function dialogManager () {
    let dialog = [],doing = false;
    return {
        clean:() => {
            dialog.length = 0;
        },
        pop:() => {
            let cb = dialog.shift();
            cb &&cb()
        },
        push:(callback) => {
            if(dialog.length == 0 && doing ==false){
                doing = true
                callback()
                return
            }
            dialog.push(callback);
            doing = false
        }
    }
}

window.dialogManager = dialogManager();


//初始化全局弹窗
(() => {
    let needWxAuth = $("#need_wx_auth").val(),
        isLogin = $("#is_login").val() === "true",
        app_client = $("#app_client").val();
    if(isLogin && needWxAuth === "true" && app_client === "wx_client"){
        window.dialogManager.push(() => {
            dialog.open({
                title:"微信授权",
                html:`<div class="text-left">每课申请您的微信头像、昵称，并开通微信提醒</div>`,
                className:"dialog-alert",
                footer:{
                    okText:"允许",
                    cancelText:"拒绝"
                },
                okEvent:function () {
                    location.href = `/common/authorize?type=userinfo`
                }
            })
        })
    }
        
})()

$(function () {

    //绑定全局登录弹窗
    $(".global-login-modal").click(function (e) {
        e.preventDefault();
        dialog.loginModal()
    })
})





